using ERPBackend.Data;
using ERPBackend.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;

namespace ERPBackend.Services
{
    public class FirstLevelCategoryService : IFirstLevelCategoryService
    {
        private readonly ApplicationDbContext _context;

        public FirstLevelCategoryService(ApplicationDbContext context)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));
        }

        public async Task<IEnumerable<FirstLevelCategory>> GetAllFirstLevelCategoriesAsync()
        {
            return await _context.FirstLevelCategories.ToListAsync() ?? Enumerable.Empty<FirstLevelCategory>();
        }

        public async Task<FirstLevelCategory?> GetFirstLevelCategoryByIdAsync(int id)
        {
            return await _context.FirstLevelCategories.FindAsync(id);
        }

        public async Task<FirstLevelCategory> CreateFirstLevelCategoryAsync(FirstLevelCategory firstLevelCategory)
        {
            // 检查分类代码是否唯一
            var existingCategory = await _context.FirstLevelCategories
                .FirstOrDefaultAsync(c => c.CategoryCode == firstLevelCategory.CategoryCode);
            if (existingCategory != null)
            {
                throw new ArgumentException("分类代码已存在");
            }
            
            _context.FirstLevelCategories.Add(firstLevelCategory);
            await _context.SaveChangesAsync();
            return firstLevelCategory;
        }

        public async Task<bool> UpdateFirstLevelCategoryAsync(FirstLevelCategory firstLevelCategory)
        {
            // 检查分类是否存在
            var existingCategory = await _context.FirstLevelCategories.FindAsync(firstLevelCategory.Id);
            if (existingCategory == null)
            {
                return false;
            }

            // 更新属性
            existingCategory.CategoryCode = firstLevelCategory.CategoryCode;
            existingCategory.CategoryName = firstLevelCategory.CategoryName;
            existingCategory.Status = firstLevelCategory.Status;

            // 可以在这里添加其他业务逻辑

            _context.Entry(existingCategory).State = EntityState.Modified;
            await _context.SaveChangesAsync();
            return true;
        }

        public async Task<bool> DeleteFirstLevelCategoryAsync(int id)
        {
            var firstLevelCategory = await _context.FirstLevelCategories.FindAsync(id);
            if (firstLevelCategory == null)
            {
                return false;
            }

            // 检查是否有相关的二级分类
            var hasSecondLevelCategories = await _context.SecondLevelCategories
                .AnyAsync(s => s.FirstLevelCategoryId == id);
            if (hasSecondLevelCategories)
            {
                // 可以根据业务需求决定是否允许删除
                // 这里简单地不允许删除
                return false;
            }

            _context.FirstLevelCategories.Remove(firstLevelCategory);
            await _context.SaveChangesAsync();
            return true;
        }
    }
}